package com.ymttest.business;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.BaseBean;
import com.ymt.base.YmatouBaseCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.JsonBeanHelper;
@Deprecated
//请尽量不要用这个类
public class YmatouAutoTestCall extends YmatouBaseCall {

	public YmatouAutoTestCall(String systemUrl, String opurl, String Mode,
			String beanFormat) {
		super(systemUrl, opurl, Mode, beanFormat);
	}

	@Override
	public void addDefHeader() {
		super.addDefHeader();
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	// for post and get
	public JSONObject callAndGetReturnData() {
		return mode.toLowerCase().equals("get")
				? callAndGetReturnData(new String[]{})
				: callAndGetReturnData(null, null);
	}

	// for post and get
	public JSONObject callAndGetReturnData(Map<String, Object> updateMap) {
		// for get type
		if (mode.toLowerCase().equals("get")) {
			String[] val = new String[updateMap.size()];
			int index = 0;
			for (String key : updateMap.keySet()) {
				val[index] = key + "=" + updateMap.get(key).toString();
				index++;
			}
			return callAndGetReturnData(val);
		} else
		// for post type
		{
			return callAndGetReturnData(null, updateMap);
		}

	}

	// for post
	public JSONObject callAndGetReturnData(String fileName) {
		return this.callAndGetReturnData(fileName, null);
	}

	/***
	 * for post 调用接口，并获取返回值 如发生错误，返回null
	 * 
	 * @return
	 */
	public JSONObject callAndGetReturnData(String fileName,
			Map<String, Object> updateMap) {

		// 获取json bean
		JSONObject jsonObject = this.getJsonBean(fileName);

		// 修改bean
		if (updateMap != null) {
			JsonBeanHelper.updateJsonBean(jsonObject, updateMap);
		}

		// 获取返回值
		return callAndGetReturnData(jsonObject);

	}

	// for post type
	public JSONObject callAndGetReturnData(JSONObject jsonBean) {

		// 设置bean
		this.setData(jsonBean);

		// 发送请求
		try {
			this.callService();
			Logger.debug(this.getAllHeader());
			// 获取返回值
			//如果返回值不是json格式， put in a json and key is 'response'
			try {
				return new JSONObject(this.getReturnData()).put(
						JsonBeanHelper.JsonBeanRequest, jsonBean);
			} catch (JSONException e) {
				Logger.comment("response is not json format");
			}
			return new JSONObject().put("response", this.getReturnData()).put(
					JsonBeanHelper.JsonBeanRequest, jsonBean);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// for post type
	public JSONObject callAndGetReturnData(BaseBean bb) {

		return callAndGetReturnData(true, bb);
	}
	// for post type
	public JSONObject callAndGetReturnData(boolean b, BaseBean bb) {

		this.setData(b, bb);
		try {
			this.callService();
			try {

				return new JSONObject(this.getReturnData());

			} catch (JSONException e) {
				Logger.comment("response is not json format");
			}

			return new JSONObject().put("response", this.getReturnData());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// for get type

	public JSONObject callAndGetReturnData(String[] args) {

		try {
			// 设置bean
			setData(StringUtils.join(args, "&"));

			// 发送请求
			this.callService();

			// 获取返回值
			return new JSONObject(this.getReturnData()).put(
					JsonBeanHelper.JsonBeanRequest, new JSONObject(
							new JSONObject(), args));

		} catch (Exception e) {
			Logger.fail(e);
		}
		return null;

	}

	/***
	 * 根据call类的路径 获取json file 如指定fileName 就用指定的，如没有指定，就用默认模板
	 * 
	 * @return
	 */
	public final JSONObject getJsonBean(String fileName) {
		String clzName = this.getClass().getName();

		// remove class name
		String rootPath = clzName.substring(0, clzName.lastIndexOf("."));

		// remove service level name
		rootPath = rootPath.substring(0, rootPath.lastIndexOf("."));

		String parentPath = this.getClass().getSimpleName();
		fileName = (fileName == null || fileName.equals("")) ? parentPath
				.toLowerCase() + "tpl.json" : fileName;

		String fullPath = rootPath + File.separator + parentPath
				+ File.separator + fileName;
		return JsonBeanHelper.getJsonBean(fullPath);

	}

	public JSONObject callAndGetReturnData(
			List<Map<String, Object>> updateMapList) {
		try {
			JSONArray jsonArray = new JSONArray();

			for (int i = 0; i < updateMapList.size(); i++) {
				JSONObject jsonObject = new JSONObject();
				for (String key : updateMapList.get(i).keySet()) {
					jsonObject.put(key, updateMapList.get(i).get(key));
				}

				jsonArray.put(i, jsonObject);
			}

			// 设置bean
			this.setData(jsonArray.toString());

			// 发送请求
			this.callService();

			// 获取返回值
			return new JSONObject(this.getReturnData()).put(
					JsonBeanHelper.JsonBeanRequest, jsonArray);

		} catch (Exception e) {
			Logger.fail(e);
		}
		return null;

	}

	public void setData(BaseBean bb) {
		this.basebean = bb;

		try {
			Logger.comment("设置参数开始:" + this.getUrl());
			Class c = bb.getClass();
			Field[] fs = c.getDeclaredFields();
			Field[] var7 = fs;
			int var6 = fs.length;

			for(int var5 = 0; var5 < var6; ++var5) {
				Field field = var7[var5];
				field.setAccessible(true);
				if (field.getType() == String[].class) {
					Logger.comment("设置数组参数:" + field.getName() + ":" + Arrays.toString((String[])field.get(bb)));
				} else if (field.getType() == List.class) {
					String jsonlist = FormatUtil.listToJSONString((List)field.get(bb));
					Logger.comment("设置List参数:" + field.getName() + ":" + jsonlist);
				} else {
					Logger.comment("设置参数:" + field.getName() + ":" + field.get(bb));
				}

				field.setAccessible(false);
			}
		} catch (Exception var12) {
			Logger.fail(var12);
		} finally {
			Logger.comment("设置参数结束");
		}

	}

}
